/**
 * 功能描述 总表格触发事件
 */
define(function (require, exports, module) {
	var store = require("store");
	var activeUser = store.get("user");
	var Dialog=require("dialog");
	var aw = require("ajaxwrapper");
	var Form = require("form");
	
    module.exports = {
    	EnterMeter : function(datas, widget){
    		var form = widget.get("form");
    		var endGrid = widget.get("endGrid");
    		widget.set("pkCheckInImplement",datas.checkInImplement.pkCIImplement);
//    		form.reset();
    			var dataForm={
        				destination: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.pkServicePoint:datas.checkInImplement.groupReserve.planItemGroup.planItem.destination.pkDestination,
        				destinationName: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name:datas.checkInImplement.groupReserve.planItemGroup.planItem.destination.name,
        				checkInDate: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate:datas.checkInImplement.groupReserve.checkInDate,
        				checkOutDate: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate:datas.checkInImplement.groupReserve.checkOutDate,
        				allianceOrgName: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name:datas.checkInImplement.groupReserve.planItemGroup.planItem.destination.name==null?datas.checkInImplement.groupReserve.planItemGroup.allianceOrg.name: datas.checkInImplement.groupReserve.planItemGroup.planItem.destination.name,
        				reserveDate: datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.operateDate:datas.checkInImplement.groupReserve.reserveDate,
        				roomNum: datas.checkInImplement.groupReserve==null?datas.roomCount:datas.checkInImplement.groupReserve.roomNum,
        				checkInStatusKey:datas.checkInStatus.key,
        				description:datas.checkInImplement.groupReserve==null?datas.checkInImplement.nonMemGroupReserve.nonMemberGroup.description:datas.checkInImplement.groupReserve.description
        		};
    		widget.set("checkInStatusKey",datas.checkInStatus.key);
    		widget.get("form").setData(dataForm);
    		aw.ajax({
				url: "api/roomregister/querymeter",
				data: {
					
					"pkCheckInImplement":datas.checkInImplement.pkCIImplement,
					
					fetchProperties: "pkRoomRegister,room.*,room.orgBuilding.*," +
							"waterMeter,hotWaterMeter,electricMeter,checkOutWaterMeter,checkOutHotWaterMeter,checkOutElectricMeter," +
							"roomStatus,checkInMeterDate,checkOutMeterDate"
				},
				dataType: "json",
				success: function(data){
					if(data.length == 0){
						return false;
					}else{
						widget.get("endGrid").setData(data);
					}
				}
			})
    		//endGrid.setData([]);
    		widget.show([".J-watergrid",".J-waterform"]).hide([".J-grid"]);
    		widget.get("subnav").show(["return"]).hide(["servicePoint","checkInStatus", "checkInDate"]);
    	},
    	UpDateMeter : function(data, widget){
    		var status = data.roomStatus.key;
    		var falg = false;
    		if(status == "NoCheckIn"){
    			falg = true;
    		}
    		var items = [];
    		if(falg){
    			items = [{
					name:"electricMeter",
					label:"电表值(度)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
					events: {
						"blur": function(e){
			        		var electricMeter = remarkForm.getValue("electricMeter");
			        		if(electricMeter){
			        			var electricMeter = parseFloat(electricMeter).toFixed(2)
				        		remarkForm.setValue("electricMeter",electricMeter);
			        		}
						}
				},
				},{
					name:"waterMeter",
					label:"冷水表值(m³)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
					 events: {
							"blur": function(e){
				        		var waterMeter = remarkForm.getValue("waterMeter");
				        		if(waterMeter){
					        		var waterMeter = parseFloat(waterMeter).toFixed(2)
					        		remarkForm.setValue("waterMeter",waterMeter);
				        		}
							}
					},
				},{
					name:"hotWaterMeter",
					label:"热水表值(m³)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
					 events: {
							"blur": function(e){
				        		var hotWaterMeter = remarkForm.getValue("hotWaterMeter");
				        		if(hotWaterMeter){
					        		var hotWaterMeter = parseFloat(hotWaterMeter).toFixed(2)
					        		remarkForm.setValue("hotWaterMeter",hotWaterMeter);
				        		}
							}
					},
				},{
					name:"checkInMeterDate",
					label:"抄表日期",
					type:"date",
					defaultValue:moment().format("YYYY-MM-DD"),
					readonly:true,
				}]
    		}else{
    			items = [{
					name:"checkOutElectricMeter",
					label:"电表值(度)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
            	   	 events: {
							"blur": function(e){
				        		var checkOutElectricMeter = remarkForm.getValue("checkOutElectricMeter");
				        		if(checkOutElectricMeter){
					        		var checkOutElectricMeter = parseFloat(checkOutElectricMeter).toFixed(2)
					        		remarkForm.setValue("checkOutElectricMeter",checkOutElectricMeter);
				        		}
							}
					},
				},{
					name:"checkOutWaterMeter",
					label:"水表值(m³)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
					 events: {
							"blur": function(e){
				        		var checkOutWaterMeter = remarkForm.getValue("checkOutWaterMeter");
				        		if(checkOutWaterMeter){
					        		var checkOutWaterMeter = parseFloat(checkOutWaterMeter).toFixed(2)
					        		remarkForm.setValue("checkOutWaterMeter",checkOutWaterMeter);
				        		}
							}
					},
				},{
					name:"checkOutHotWaterMeter",
					label:"热水表值(m³)",
					validate : ["required"],
					exValidate: function(value){
						var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; 
						if(!reg.test(value)){
					    	return "请输入正数或0";
					    }else{
					    	return true;
					    }
					},
					 events: {
							"blur": function(e){
				        		var checkOutHotWaterMeter = remarkForm.getValue("checkOutHotWaterMeter");
				        		if(checkOutHotWaterMeter){
					        		var checkOutHotWaterMeter = parseFloat(checkOutHotWaterMeter).toFixed(2)
					        		remarkForm.setValue("checkOutHotWaterMeter",checkOutHotWaterMeter);
				        		}
							}
					},
				},{
					name:"checkOutMeterDate",
					label:"抄表日期",
					type:"date",
					defaultValue:moment().format("YYYY-MM-DD"),
					readonly:true,
				}]
    		};
    		var remarkForm =new Form({
				parentNode:".J-form-dialog",
				autoRender : false,
        		model:{
					id:"remarkForm",
					defaultButton:false,
					items:items
				 }
			});
    		
			widget.set("remarkForm",remarkForm);
			Dialog.showComponent(remarkForm,{
				defaultButton:false,
				title: "抄表",
				setStyle: function(){
					$(".el-dialog-modal .modal").css({
						"top": "10%",
						"width":"50%",
						"left": "25%",
					});
				},
				buttons :[{
					id:"save",
					text:"保存",
					className:"btn-primary",
					handler:function(){
						if(remarkForm.valid()){
							var FormData = remarkForm.getData();
							var saveData = {};
							saveData.pkRoomRegister = data.pkRoomRegister;
							saveData.roomStatus=status;
							if(falg){
								saveData.electricMeter = FormData.electricMeter;
								saveData.waterMeter = FormData.waterMeter;
								saveData.hotWaterMeter = FormData.hotWaterMeter;
								saveData.checkInMeterDate =FormData.checkInMeterDate;
							}else{
								saveData.checkOutElectricMeter = FormData.checkOutElectricMeter;
								saveData.checkOutWaterMeter = FormData.checkOutWaterMeter;
								saveData.checkOutHotWaterMeter = FormData.checkOutHotWaterMeter;
								saveData.checkOutMeterDate =FormData.checkOutMeterDate;
							}
							aw.saveOrUpdate("api/roomregister/savemeter",aw.customParam(saveData),function(saveData){
								Dialog.close();
								aw.ajax({
									url: "api/roomregister/querymeter",
									data: {
										"pkCheckInImplement":widget.get("pkCheckInImplement"),
										fetchProperties: "pkRoomRegister,room.*,room.orgBuilding.*," +
												"waterMeter,hotWaterMeter,electricMeter,checkOutWaterMeter,checkOutHotWaterMeter,checkOutElectricMeter," +
												"roomStatus,checkInMeterDate,checkOutMeterDate"
									},
									dataType: "json",
									success: function(data){
										if(data.length == 0){
											return false;
										}else{
											if(falg){
												var j=0;
												for(var i=0;i<data.length;i++){
													if(data[i].roomStatus.key=="CheckIn"){
														j++;
													}
												}
												if(j==data.length){
													widget.set("checkInStatusKey","CheckIn");
												}
											}
											widget.get("endGrid").setData(data);
										}
									}
								})
								
			       			});
						};
					}
				},{
					id:"close",
					text:"取消",
					handler:function(){
						Dialog.close();
					}
				}]
			});
    	},
    };
});
